import java.util.Arrays;
public class Main {
    //自定义toString函数
    public static String myToString(int[] array){
        String ret="[";
        for (int i = 0; i < array.length; i++) {
            ret+=array[i];
            if(i!=array.length-1){
                ret+=',';
            }
        }
        ret+="]";
        return ret;
    }

    public static int[] func3(){
        return new int[]{1,2,3,4,5,6,7,8,9};
        //作为函数返回值
    }
    public static void func1(int[] array){
        array=new int[10];
    }
    public static void func2(int[] array){
        array[0]=10;
    }
    public static int binarySearch(int[] array,int key){
        int left=0;
        int right=array.length-1;
        while (left <= right) {
            int mid=(left+right)/2;
            if(array[mid]<key){
                left=mid+1;
            }else if(array[mid]>key){
                right=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        //i代表的是趟数
        for (int i = 0; i <array.length ; i++) {
            //j代表每一趟比较的次数
            boolean flg=false;
            for (int j = 0; j < array.length-i-1; j++){
               if(array[j]>array[j+1]){
                   int tmp=array[j];
                   array[j]=array[j+1];
                   array[j+1]=tmp;
                   flg=true;
               }
            }
            if(flg==false){
                break;//说明数组有序了
            }
        }
    }

    public static void main(String[] args) {
        int[] array={3,67,45,91,66,32,5};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main9(String[] args) {
        int[] array3=new int[10];
        Arrays.fill(array3,2);//这个函数可以直接帮你填充数据
        System.out.println(Arrays.toString(array3));
    }
    public static void main8(String[] args) {
        int[] arr1={1,4,5,7};
        int[] arr2={1,4,5,7};
        boolean flg=Arrays.equals(arr1,arr2);//这个函数功能是比较2个数字对应位置数据是否一样
        System.out.println(flg);
    }
    public static void main7(String[] args) {
        int[] array={20,78,56,23,-6};
        Arrays.sort(array);//java工具包的排序函数
        System.out.println(Arrays.toString(array));
        //System.out.println(Arrays.binarySearch(array, key));
        //上面是java工具包的二分查找函数，可直接使用
        System.out.println(binarySearch(array,23));
    }
    public static void main6(String[] args) {
        int[] array={1,3,5,7,9};
        int[] copy=new int[array.length];
        //本地拷贝方法，高效快捷
        //public static native void arraycopy(源数组，源数组开始拷贝的位置，目的数组，目的数组的哪个位置，拷贝的长度）
        System.arraycopy(array,0,copy,0,array.length);
        //System.out.println(Arrays.toString(copy));
    }
    public static void main5(String[] args) {
        int[] array={1,3,5,7,9};
        //拷贝函数
        int[] copy=Arrays.copyOf(array,array.length);
        //int[] copy=Arrays.copyOf(array,array.length*2);
        //*2相当于给数组扩容
        System.out.println(Arrays.toString(copy));
        int[] copy2=Arrays.copyOfRange(array,1,4);
        System.out.println(Arrays.toString(copy2));//[3,5,7]
        //java中范围是左闭右开  [a,b)
        //若超出范围，能拷贝多少拷贝多少
    }
    public static void main4(String[] args) {
        int[] array={2,4,6,8};
        String ret=myToString(array);
        System.out.println(ret);
    }
    public static void main3(String[] args) {
        int[] ret=func3();
        //借助工具类，将参数的数组转化为字符串
        String s=Arrays.toString(ret);
        System.out.println(s);
//        for(int i=0;i<ret.length;i++) {
//            System.out.print(ret[i] + " ");
//        }

    }
    public static void main2(String[] args) {
        int[] array={3,5,7,9};
        func1(array);
        int i=0;
        for(i=0;i<array.length;i++){
            System.out.print(array[i]+" ");//3 5 7 9
        }
        System.out.println();
        func2(array);
        for(i=0;i<array.length;i++){
            System.out.print(array[i]+" ");//10 5 7 9
        }
    }
    public static int max(int a,int b){
        return a>b?a:b;
    }
    public static int max(int a,int b,int c){
        int max=max(a,b);
        return max(max,c);
    }
    public static void main1(String[] args) {
        int[] array={3,5,7,9};
        int i=0;
        for(i=0;i<array.length;i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
        for(int a:array){
            System.out.print(a);
        }
    }
}